package com.yili.oa.service.impl;

import com.yili.oa.dao.SignInMapper;
import com.yili.oa.entity.SearchBase;
import com.yili.oa.models.SignIn;
import com.yili.oa.models.SignInExample;
import com.yili.oa.service.SignInService;
import com.yili.oa.utils.Exception.AjaxException;
import com.yili.oa.utils.ListUtils;
import com.yili.oa.utils.annotate.DataBase;
import com.yili.oa.vo.request.SignInReq;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * @author lxr
 * @version v0.01
 * @date 2019/7/22 0022
 * @email 200890949@qq.com
 */

@SuppressWarnings("ALL")
@Service
@DataBase("lyt_game_db")
@Transactional(value = "transactionManager")
public class SignInSvImpl implements SignInService {

    @Autowired
    private SignInMapper mapper;

    @Override
    public List<SignIn> getList(SearchBase searchBase) {
        SignInExample example = new SignInExample();
        SignInExample.Criteria criteria = example.createCriteria();
        if (searchBase.getId()!=null){
            criteria.andIdEqualTo(searchBase.getId());
        }
        example.setOrderByClause("day ASC");
        return mapper.selectByExample(example);
    }

    @Override
    public void add(SignInReq signInReq) {
        SignIn signIn = new SignIn();
        BeanUtils.copyProperties(signInReq,signIn);
        signIn.setId(UUID.randomUUID().toString());
        try {
            mapper.insert(signIn);
        }catch (Exception e){
            throw new AjaxException("添加失败");
        }
    }

    @Override
    public void update(SignInReq signInReq) {
        SignIn signIn = mapper.selectByPrimaryKey(signInReq.getId());
        BeanUtils.copyProperties(signInReq,signIn);
        try{
            mapper.updateByPrimaryKey(signIn);
        }catch (Exception e){
            throw new AjaxException("更新失败");
        }
    }

    @Override
    public void delSignIn(List<String> list) {
        SignInExample example = new SignInExample();
        SignInExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(list);
        try {
            mapper.deleteByExample(example);
        }catch (Exception e){
            throw new AjaxException("删除失败");
        }
    }

    @Override
    public boolean checkDay(Integer day, String id) {
        SignInExample example = new SignInExample();
        SignInExample.Criteria criteria = example.createCriteria();
        criteria.andDayEqualTo(day);
        if (id!=null){
            criteria.andIdNotEqualTo(id);
        }
        List<SignIn> list = mapper.selectByExample(example);
        if (ListUtils.isNotNullOrEmpty(list)){
            return false;
        }
        return true;
    }
}
